package cn.wizzer.app.web.commons.ext.websocket.action.msg.attendant;

import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.filter.AttendantFilter;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.base.TextWebSocketFrameHandler;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.param.Param;
import cn.wizzer.framework.util.param.Param4Map;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

@IocBean
@NettyAction(uri = "/attendant/close", filters = {TokenFilter.class, AttendantFilter.class})
public class CloseAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserService userService;

    /**
     * 关闭会话
     *
     * @param income
     * @param params
     * @return
     */
    @Override
    public Result action(Channel income, @Param4Map(fields = {@Param(name = "target", required = true),
            @Param(name = "assignAttendant")}) JSONObject params) {
        String userId = params.getString("appid");
        ChatUserVO self = NettyCache.globalGet(userId);
        String targetId = params.getString("target");
        if (self.getUsers().contains(targetId)) {
            //指定用户在线的情况下。判断是否将用户转交给指定在线客服
            ChatUserVO target = NettyCache.globalGet(targetId);
            if (target != null) {
                String assignAttendant = params.getString("assignAttendant");
                //用户channel
                Channel channel = TextWebSocketFrameHandler.findChannel(target.getChannel().id().toString());
                //通知
                NettyMsgUtil.send(channel, ChatResult.MessageTypeEnum.SYSTEM, ChatResult.ActionEnum.LEAVE, self, null);
                if (Strings.isNotBlank(assignAttendant)) {
                    ChatUserVO assignAttendantUserVO = NettyCache.globalGet(assignAttendant);
                    if (assignAttendantUserVO == null) {
                        return Result.error("指定客服已离线");
                    }
                    //指定客服用户列表中添加该用户
                    assignAttendantUserVO.getUsers().add(targetId);
                    //用户指定客服变更
                    NettyCache.localSet(channel, "targetAttendant", assignAttendantUserVO.getId());
                    //通知新加入客服
                    NettyMsgUtil.send(channel, ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.JOIN, assignAttendantUserVO, null);
                } else {
                    //移除用户指定客服
                    NettyCache.localRemove(target.getChannel(), "targetAttendant");
                }
            }
            //客服列表移除当前用户
            self.getUsers().remove(targetId);
            return Result.success();
        }
        return Result.error("没有对应的用户");
    }


}
